#include "Surface.h"

Surface::Surface(LPDIRECT3DDEVICE9 d3ddv, LPDIRECT3DSURFACE9 backBuffer, LPWSTR file_name)
{
	this->d3ddv = d3ddv;
	this->backBuffer = backBuffer;

	this->file_name = file_name;
	this->Load();
}

Surface::~Surface()
{
	if(this->surface)
	{
		this->surface->Release();
	}
}

void Surface::Draw(const RECT* p_sRect, const RECT* p_dRect)  
{ 
	this->d3ddv->StretchRect(
		this->surface,		// from , gameworld 
		p_sRect,				// which portion?
		this->backBuffer,		// to , backbuffer
		p_dRect,				// which portion?
		D3DTEXF_NONE);
}

void Surface::Load()
{
	D3DXIMAGE_INFO info; 
	//Lay thong tin file anh
	HRESULT result = D3DXGetImageInfoFromFile(file_name, &info);
	if (result != D3D_OK)
	{
		trace(L"[ERROR] Failed to get image info '%s'", file_name);
		return;
	}
	this->d3ddv->CreateOffscreenPlainSurface(
		info.Width,				//width of the surface
		info.Height,			//height of the surface
		D3DFMT_X8R8G8B8,		//surface format
		D3DPOOL_DEFAULT,		//memory pool to use
		&surface,				//pointer to the surface
		NULL);					//reserved (always NULL)

	result = D3DXLoadSurfaceFromFile(
		surface, 					// destanation surface
		NULL,						// destination palette	
		NULL,						// destination rectangle 
		file_name,					// source filename
		NULL,						// source rectangle
		D3DX_DEFAULT, 				// filter image
		D3DCOLOR_XRGB(0,0,0),		// transparency (0 = none)
		NULL);						// source image info (usually NULL)

	if (result != D3D_OK)
	{
		trace(L"[ERROR] D3DXLoadSurfaceFromFile() failed");
		return;
	}
} 